<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Car;
use App\Models\User;
use App\Models\Order;
use Session;
use Carbon\Carbon;

class OrderController extends Controller
{
    /**
     * 创建订单
     */
    public function create(Request $request)
    {
        if(!session('user')) {
            session(['car_id'=>$request->car_id]);
            return redirect('login');
        }
    	$rs['status'] = true;
    	$car_id = session('car_id')?session('car_id'):$request->car_id;
    	$car = Car::find($car_id);
        $car->status = 'reserved';
        $car->save();
    	$data['user_id']    = session('user')->id;
    	$data['car_id']     = $car_id;
    	$data['booking_at'] = date('Y-m-d H:i:s');
        $data['car_model'] = $car->model;
        $data['car_code']  = object_get($car,'code');
        $data['car_type']  = object_get($car,'type.name');
        $data['car_price'] = object_get($car,'type.price');
        $data['pre_pay']   = $request->prepay;
    	$rs['data']   = Order::create($data);
        session(['car_id'=>null]);
    	return redirect('mybooking');
    	
    }

    public function pay(Request $request)
    {
    	$rs['status'] = true;
    	$id = (int)$request->id;
        $data = Order::find($id);
        return view('sharecar.order-pay',compact('data'));
    }

    public function mybooking(Request $request)
    {
        if(!session('user')) {
            return redirect('login');
        }
        $where['user_id'] = session('user')->id;
        $list = Order::where($where)->orderBy('id','desc')->paginate(10);
        return view('sharecar.mybooking',compact('list'));
    }

    public function cancel(Request $request)
    {
        $order_id = $request->id;
        $order = Order::find($order_id);
        $order->delete();
        $car = Car::find($order->car_id);
        $car->status = 'available';
        $car->save();
        return back();

    }

    public function complete(Request $request)
    {
        $now = time();
        $id = (int)$request->id;
        $order = Order::find($id);
        $order->completed_at =  date('Y-m-d H:i:s');

        $start = strtotime($order->picked_at);
       
        $minutes = round(($now-$start)/60);
        $order->minutes = $minutes;
        $order->money   = $minutes*$order->car_price; 
        $order->save();

        $car = Car::find($order->car_id);
        $car->status = 'available';
        $car->lnglat = $request->lnglat;
        $car->save();
        $rs['status'] = true;
        $rs['data']   = null;
        return response()->json($rs);
        
    }

    public function pickup(Request $request)
    {
        $id = (int)$request->id;
        $order = Order::find($id);
        $order->picked_at = date('Y-m-d H:i:s');
        $order->save();

        $car = Car::find($order->car_id);
        $car->status = 'unavailable';
        $car->save();
        return back();

    }

    public function show(Request $request)
    {
        $id = (int)$request->id;
        $data = Order::find($id);
        return view('sharecar.order-show',compact('data'));
    }



    
}
